<?php

require_once PATH_CORE . '/../lib/mpdf/mpdf.php';

class PdfController extends MainController implements ControllerInterface {

	public $tableConfig = array(
		'product_table' => array(
			'o' => array(
				'id' => 'product.id_product',
				'code' => 'product.code',
				'name' => 'product_description.name',
				'_' => 'id'
			),
			'v' => 'malejaco',
			't' => 10
		),
		'infopts_table' => array(
			'o' => array(
				'id' => 'image_map.id_image_map',
				'name' => 'image_map_description.title',
				'_' => 'id'
			),
			'v' => 'malejaco',
			't' => 2
		)
	);

	public function ProductDetailsAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
		$this->SetNoRender();
		if (!$this->userLoggedIn) {
			$this->AddRedirectInfo(Utils::Translate('message sent',Registry::Get('_user')), 'ok', Router::GenerateUrl('Index', 'Index' ));
		}
		try {
			$this->AddDescription("");
			$this->AddTitle("");

			$productObj = $this->GetProduct(array('value' => $param['productKey']));

			$id = $productObj->getId();
			$this->smarty->assign('obj', $productObj);
			$dalDataC = CategoryDAL::GetDalDataObj();
			$dalDataC->setId($productObj->getIdCategory());
			$category = CategoryDAL::GetById($dalDataC);
			
			$dalObj = PropertyValueDAL::GetDalDataObj();
			$dalObj->addCondition('id_product', $productObj->getId());
			$dalObj->addJoin('Property', ' JOIN property ON (property_value.id_product = ' . $productObj->getId() . ' AND property_value.id_property = property.id_property AND property.active = 1) ');
			$dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = ' . Registry::Get('default_language_id') . ')');
			// $dalObj->setGroupBy('property.id_property');
			 $dalObj->addCondition('property.id_property_group', '6');
			 $dalObj->addCondition('(property.id_category', $productObj->getIdCategory());
			 $dalObj->addCondition('property.id_category', $category->getParent(), ' = ', ' OR ');
			 $dalObj->addCondition('1', 1, ' = ', ') AND');
			 $dalObj->setOrderBy('property.position ASC');
			 //$dalObj->addCondition('property_value.type', Registry::Get('METRIC_SYSTEM_TYPE'));
			 $properties10 = PropertyValueDAL::GetResult($dalObj);
			 
			 $properties1 = array();
			 foreach($properties10 as $prop) {
				 if(!isset($properties1[$prop->property->getId()]))
				 $properties1[$prop->property->getId()] = $prop;
				 else
					 $properties1[$prop->property->getId()]->setValue2($prop);
			 }

			 $dalObj = PropertyValueDAL::GetDalDataObj();
			 $dalObj->addCondition('id_product', $productObj->getId());
			 $dalObj->addJoin('Property', ' JOIN property ON (property_value.id_product = '.$productObj->getId().' AND property_value.id_property = property.id_property AND property.active = 1) ');
			 $dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = '.Registry::Get('default_language_id').')');
			 //$dalObj->setGroupBy('property.id_property');
			 $dalObj->addCondition('property.id_property_group', '7');
			 $dalObj->addCondition('(property.id_category', $productObj->getIdCategory());
			 $dalObj->addCondition('property.id_category', $category->getParent(), ' = ', ' OR ');
			 $dalObj->addCondition('1', 1, ' = ', ') AND');
			 $dalObj->setOrderBy('property.position ASC');
			 //$dalObj->addCondition('property_value.type', Registry::Get('METRIC_SYSTEM_TYPE'));
			 $properties20 = PropertyValueDAL::GetResult($dalObj);
			 $properties2 = array();
			 foreach($properties20 as $prop) {
				 if(!isset($properties2[$prop->property->getId()]))
				 $properties2[$prop->property->getId()] = $prop;
				 else
					 $properties2[$prop->property->getId()]->setValue2($prop);
			 }

			 $dalObj = PropertyValueDAL::GetDalDataObj();
			 $dalObj->addCondition('id_product', $productObj->getId());
			 $dalObj->addJoin('Property', ' JOIN property ON (property_value.id_product = '.$productObj->getId().' AND property_value.id_property = property.id_property AND property.active = 1) ');
			 $dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = '.Registry::Get('default_language_id').')');
			 $dalObj->addCondition('property.id_property_group', '5');
			 $dalObj->addCondition('(property.id_category', $productObj->getIdCategory());
			 $dalObj->addCondition('property.id_category', $category->getParent(), ' = ', ' OR ');
			 $dalObj->addCondition('1', 1, ' = ', ') AND');
			 $dalObj->setOrderBy('property.position ASC');
			 $properties50 = PropertyValueDAL::GetResult($dalObj);

			$properties5 = array();
			foreach ($properties50 as $prop) {
				if (!isset($properties5[$prop->property->getId()]))
					$properties5[$prop->property->getId()] = $prop;
				else
					$properties5[$prop->property->getId()]->setValue2($prop);
			}

			$this->smarty->assign('properties1', $properties1);
			$this->smarty->assign('properties2', $properties2);
			$this->smarty->assign('properties5', $properties5);




			$this->smarty->assign('subCategoryKey', $param['subCategoryKey']);
			$this->smarty->assign('categoryKey', $param['categoryKey']);

			$dalObj = CategoryDAL::GetDalDataObj();
			$dalObj->addCondition('category.id_category', $productObj->getIdCategory());
			$dalObj->addJoin('CategoryDescription', ' LEFT JOIN category_description ON (category.id_category = category_description.id_category AND category_description.id_language = 1)');
			$category = CategoryDAL::GetResult($dalObj);
			$this->smarty->assign('category', $category[0]);

			$dalObj = CategoryDAL::GetDalDataObj();
			$dalObj->addCondition('category.id_category', $category[0]->getParent());
			$dalObj->addJoin('CategoryDescription', ' LEFT JOIN category_description ON (category.id_category = category_description.id_category AND category_description.id_language = 1)');
			$categoryP = CategoryDAL::GetResult($dalObj);
			$this->smarty->assign('category', $categoryP[0]);

			$dalObj = ProductImageDAL::GetDalDataObj();
			$dalObj->addCondition('active', '1');
			$dalObj->addCondition('id_product', $productObj->getId());
			$images = ProductImageDAL::GetResult($dalObj);

			$this->smarty->assign('images', $images);

			$dalObj = ProductVideoDAL::GetDalDataObj();
			$dalObj->addCondition('active', '1');
			$dalObj->addCondition('id_product', $productObj->getId());
			$videos = ProductVideoDAL::GetResult($dalObj);

			$this->smarty->assign('videos', $videos);

//			$dalObjm = ImageMapDAL::GetDalDataObj();
//			$dalObjm->addCondition('id_product', $id);
//			$dalObjm->addCondition('active', '1');
//			$dalObjm->addJoin('ImageMapDescription', ' LEFT JOIN image_map_description ON (image_map.id_image_map = image_map_description.id_image_map AND image_map_description.id_language = 1)');
//			$imageMap = ImageMapDAL::GetResult($dalObjm);
//
//			$this->smarty->assign('map', $imageMap);



			$this->smarty->assign('lang_id', Registry::Get('default_language_id'));
		} catch (Exception $e) {
			$this->AddRedirectInfo($this->T('productViewEmpty'));
		}

		$html = $this->smarty->fetch('partial/Pdf/ProductDetails.tpl');
		$pdf = new mPdf('utf-8', 'A4', 11, 15, 15, 16);
		$this->Output($pdf, $html, $productObj->getSlug());
		unset($pdf);
	}

	public function ProductComparisonAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");

		$this->SetNoRender();
		
		$this->smarty->assign('listOfProducts', $param['listOfProducts']);

		if(isset($param['listOfProducts'])) {
			$productsArray = explode('_', $param['listOfProducts']);
			$x=0;
			foreach($productsArray as $pk) {
				if($pk=='') {
					unset($productsArray[$x]);
				}
				$x++;
			}

		$dalObj = ProductDAL::GetDalDataObj();
		 $dalObj->addCondition('product.id_product', '("'.implode('","', $productsArray).'")', ' IN ');
		 $dalObj->addJoin('ProductDescription', ' LEFT JOIN product_description ON(product.id_product = product_description.id_product AND product_description.id_language = '.Registry::Get('default_language_id').')');
		 $dalObj->addJoin('Manufacturer' , ' LEFT JOIN manufacturer ON (product.id_manufacturer = manufacturer.id_manufacturer AND manufacturer.active = 1)');
		 $products = ProductDAL::GetResult($dalObj);


			 $dalObj = PropertyDAL::GetDalDataObj();
			 $dalObj->addJoin('PropertyValue', ' LEFT JOIN property_value ON (property_value.id_property = property.id_property) ');
			 $dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = '.Registry::Get('default_language_id').')');
			 $dalObj->addCondition('active', '1');
			 $dalObj->addCondition('property_value.id_product', ' ("'.implode('","', $productsArray).'") ', ' IN ');
			 $dalObj->addCondition('property_value.type', Registry::Get('METRIC_SYSTEM_TYPE'));
			 $dalObj->setGroupBy('property.id_property');
			 $properties = PropertyDAL::GetResult($dalObj);

		 foreach($products as $product) {
			 $dalObj = PropertyValueDAL::GetDalDataObj();
			 $dalObj->addCondition('id_product', $product->getId());
			 $dalObj->addJoin('Property', ' JOIN property ON (property_value.id_product = '.$product->getId().' AND property_value.id_property = property.id_property AND property.active = 1) ');
			 $dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = '.Registry::Get('default_language_id').')');
			 $dalObj->setGroupBy('property.id_property');
			 $product->setProperties(PropertyDAL::GetResult($dalObj));
		 }

		}

		// drobna zmiana przy generowaniu tabelek

		$_rowsMaxLength = array(
			0, 0, 0, 0, 0
		);
		
		$_i = 5;

		$_tableData = array(
			array(0 => '&nbsp;'),
			array(0 => 'Product name'),
			array(0 => 'Brand name'),
			array(0 => 'Popularity'),
			array(0 => 'Rate')
		);

		$_var_config_arr = array();
		
		foreach ($properties as $item) {
			$_tableData[$_i] = array(0 => $item->getPropertyDescription()->getDescription());
			$_var_config_arr[$item->getId()] = $_i;
			$_i += 1;
		}
		$_tableData[$_i] = array('Price');
		
		$pc = 1;
		$productsArray = array(0 => null);
		foreach ($products as $_product) {
			$productsArray[$pc] = $_product;
			$_tableData[0][$pc] = $product->getMainImage();
			$_svar = $_product->getProductDescription()->getName();
			$_tableData[1][$pc] = $_svar;
			$_rowsMaxLength[1] = (mb_strlen($_svar) > $_rowsMaxLength[1]?mb_strlen($_svar):$_rowsMaxLength[1]);
			$_svar = $_product->getManufacturer()->getName();
			$_tableData[2][$pc] = $_svar;
			$_rowsMaxLength[2] = (mb_strlen($_svar) > $_rowsMaxLength[2]?mb_strlen($_svar):$_rowsMaxLength[2]);
			$_tableData[3][$pc] = $_product->getPopularity();
			$_tableData[4][$pc] = $_product->getRate();
			
			for($ii = 5; $ii <= $_i; $ii++)
			{
				$_tableData[$ii][$pc] = null;
			}
			
			$_j = 5;
			foreach ($_product->getProperties() as $_property) {
				$_svar = $_property->getPrepend() . ' ' . $_property->getValue() . ' ' . $_property->getAppend();
				$_tableData[$_var_config_arr[$_property->getIdProperty()]][$pc] = $_svar;
				if(!isset($_rowsMaxLength[$_j])) $_rowsMaxLength[$_j] = 0;
				$_rowsMaxLength[$_j] = (mb_strlen($_svar) > $_rowsMaxLength[$_j]?mb_strlen($_svar):$_rowsMaxLength[$_j]);
				$_j += 1;
			}
			$_tableData[$_i][$pc] = '€80';
			$pc += 1;
		}


		$this->smarty->assign('tableData', $_tableData);
		$this->smarty->assign('_rowsMaxLength', $_rowsMaxLength);
		$this->smarty->assign('productsArray', $productsArray);

		$this->smarty->assign('specialClasses', array(0 => 'thumb', 1 => 'title', 2 => 'brand', 3 => 'popularity', 4 => 'rate', $_i => 'itemPrice'));


		$this->smarty->assign('products', $products);
		$this->smarty->assign('properties', $properties);
		
		$this->smarty->assign('subCategoryKey', $param['subCategoryKey']);
		$dalObj = CategoryDAL::GetDalDataObj();
		$dalObj->addCondition('slug', $param['subCategoryKey']);
		$dalObj->addJoin('CategoryDescription', ' LEFT JOIN category_description ON (category.id_category = category_description.id_category AND category_description.id_language = 1)');
		$result = CategoryDAL::GetResult($dalObj);
		if(isset($result[0])) {
			$this->smarty->assign('subCategory', $result[0]);
			$this->smarty->assign('category', $result[0]->getParentCategoryObj());
		}

		$html = $this->smarty->fetch('partial/Pdf/ProductComparison.tpl');
		
//		die($html);
		
		$pdf = new mPDF('utf-8', 'A4-L', 11, 15, 15, 16);
		$this->Output($pdf, $html, $this->T('compare-products-tennizoom'));
		unset($pdf);
	}

	private function GetProduct($condition) {
		try {

			$productObj = ProductDAL::GetResult(ProductDAL::GetDalDataObj(array(
								'tableData' => $this->tableParams['product_table'],
								'join' => array(
									'ProductDescription' => ' LEFT JOIN product_description ON(product.id_product = product_description.id_product AND product_description.id_language = ' . Registry::Get('default_language_id') . ')',
									'Manufacturer' => ' LEFT JOIN manufacturer ON (product.id_manufacturer = manufacturer.id_manufacturer AND manufacturer.active = 1)'
								),
								'condition' => array('product.slug' => array($condition['value']))
							)));
			if (is_array($productObj)) {
				$productObj = $productObj[0];

				$dalObj = PropertyDAL::GetDalDataObj();
				$dalObj->addJoin('PropertyValue', ' LEFT JOIN property_value ON (property_value.id_property = property.id_property) ');
				$dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = ' . Registry::Get('default_language_id') . ')');
				$dalObj->addCondition('active', '1');
				$dalObj->addCondition('property_value.id_product', $productObj->getId());
				$dalObj->addCondition('property_value.type', Registry::Get('METRIC_SYSTEM_TYPE'));
				// $dalObj->addCondition('property.id_property_group', '6');
				$dalObj->setGroupBy('property.id_property');
				$properties1 = PropertyDAL::GetResult($dalObj);

				$dalObj = PropertyDAL::GetDalDataObj();
				$dalObj->addJoin('PropertyValue', ' LEFT JOIN property_value ON (property_value.id_property = property.id_property) ');
				$dalObj->addJoin('PropertyDescription', ' LEFT JOIN property_description ON(property.id_property = property_description.id_property AND property_description.id_language = ' . Registry::Get('default_language_id') . ')');
				$dalObj->addCondition('active', '1');
				$dalObj->addCondition('property_value.id_product', $productObj->getId());
				$dalObj->addCondition('property_value.type', Registry::Get('METRIC_SYSTEM_TYPE'));
				// $dalObj->addCondition('property.id_property_group', '7');
				$dalObj->setGroupBy('property.id_property');
				$properties2 = PropertyDAL::GetResult($dalObj);
			}


			return $productObj;
		} catch (Exception $e) {
			$this->AddRedirectInfo($this->T('productViewEmpty'));
		}
	}

	public function IndexAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
	}

	/**
	 *
	 * @param string $html 
	 * @param array $mpdf_init_values 
	 */
	private function Output($pdf, $html, $filename) {
		$pdf->SetHTMLHeader($this->getHeader());
		$pdf->SetHTMLFooter($this->getFooter());
		$pdf->WriteHtml($html);
		$pdf->Output($filename, 'D');

		exit;
	}

	private function getHeader() {
		return $this->smarty->fetch('partial/Pdf/Shared/header.tpl');
	}

	private function getFooter() {
		$this->smarty->assign('now', microtime(true));
		return $this->smarty->fetch('partial/Pdf/Shared/footer.tpl', md5('pdf'.microtime(true)), md5('pdf'.microtime(true)));
	}

	private function acl() {
		
	}

	public function PreDispatch($param) {
		$this->Run($param);

		$this->smarty->assign('pathStatic', Config::Get('PATH_STATIC_CONTENT'));
	}

	public function PostDispatch($param) {
		
	}

}

?>